package com.vhdadw.service.serciveImpl;

import com.vhdadw.component.SmsComponent;
import com.vhdadw.config.SmsConfig;
import com.vhdadw.constant.RedisKey;
import com.vhdadw.enums.BizCodeEnum;
import com.vhdadw.enums.SendCodeEnum;
import com.vhdadw.service.NotifyService;
import com.vhdadw.utils.CheckUtil;
import com.vhdadw.utils.CommonUtil;
import com.vhdadw.utils.JsonData;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class NotifyServiceImpl implements NotifyService {

    //10分钟有效
    private static final int CODE_EXPIRED = 60 * 1000 * 10;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    SmsComponent smsComponent;

    @Autowired
    private SmsConfig smsConfig;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public JsonData sendCode(SendCodeEnum sendCodeEnum, String to) {

        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY, sendCodeEnum.name(), to);

        String cacheValue = redisTemplate.opsForValue().get(cacheKey);

        //如果不为空，再判断是否是60秒内重复发送
        if (StringUtil.isNotBlank(cacheValue)) {
            Long ttl = Long.parseLong(cacheValue.split("_")[1]);
            //当前时间戳-验证码发送时间戳 <60s 不发送
            Long leftTime = CommonUtil.getCurrentTimestamp() - ttl;
            if (leftTime < 1000 * 60) {
                log.info("重复发送短信验证码，时间间隔：{}秒", leftTime);
                return JsonData.buildResult(BizCodeEnum.CODE_LIMITED);
            }
        }

        String code = CommonUtil.getRandomCode(6);

        //生成拼接好的验证码
        String value = code + '_' + CommonUtil.getCurrentTimestamp();
        redisTemplate.opsForValue().set(cacheKey, value, CODE_EXPIRED, TimeUnit.MILLISECONDS);

        //邮箱
        if (CheckUtil.isEmail(to)) {
            //发送邮箱验证码 TODO

        } else if (CheckUtil.isPhone(to)) {
            //发送手机号验证码
            String param = "**code**:" + code + ",**minute**:" + 5;
            System.out.println("没毛病！！！！！");
            System.out.println(code);
//            smsComponent.send(to, smsConfig.getTemplateId(), smsConfig.getSmsSignId(), param);
        }

        return JsonData.buildSuccess();
    }

    /**
     * 验证码校验逻辑
     * @param sendCodeEnum
     * @param to
     * @param code
     * @return
     */
    @Override
    public boolean checkCode(SendCodeEnum sendCodeEnum, String to, String code) {

        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY, sendCodeEnum.name(), to);

        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        if(StringUtils.isNotBlank(cacheValue)){
            String cacheCode = cacheValue.split("_")[0];
            if(cacheCode.equals(code)){
                //删除验证码
                redisTemplate.delete(cacheCode);
                return true;
            }
        }

        return false;
    }
}
